package com.lixw.langchain.controller;

import com.lixw.langchain.service.ChatAssistant;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.document.parser.TextDocumentParser;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: RAGController
 * @Description:
 * @Author: xuweiLi
 * @Create: 2025/8/25 20:09
 **/
@Slf4j
@RestController
public class RAGController {
    @Resource
    private InMemoryEmbeddingStore<TextSegment> embeddingStore;

    @Resource
    private ChatAssistant chatAssistant;

    @GetMapping("/rag/add")
    public String add() {
        Document document = FileSystemDocumentLoader.loadDocument("D:\\workspase\\me\\langchain4j_project\\langchain4j-13chat-rag01\\src\\main\\resources\\code.txt",new TextDocumentParser());
        EmbeddingStoreIngestor.ingest(document, embeddingStore);

        String result = chatAssistant.chat("状态码1005 和 2004 分别都是什么意思？");
        log.info("rag result: {}", result);

        return result;
    }
}